var config = require("../../config");

//获取应用实例
var app = getApp()
Page({
  data: {
    userInfo: {},
    input_length: 0,
    book_id: -1
  },
  onLoad: function (options) {
    console.log('onLoad')
    var that = this;
    that.setData({
      userInfo: app.globalData.userInfo,
      book_id: options.book_id
    })
  },

  formSubmit: function (e) {
    var that = this;
    var data = e.detail.value;
    console.log(data);
    var userInfo = that.data.userInfo;
    var note = data.note;

    if (note == '') {
      wx.showToast({
        title: '你还未输入内容',
        icon: 'success',
        duration: 1000
      })
      return;
    }

    wx.request({
      url: config.urls.smallnote_new,
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        book_id: that.data.book_id,
        user_id: userInfo.id,
        user_name: userInfo.nickname,
        content: note
      },

      success: function (res) {
        if (res.data.success) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 1500
          })
          wx.navigateBack({
            delta: 1
          })
        }
        else {
          wx.showToast({
            title: '修改失败',
            icon: 'loading',
            duration: 1500
          })
          console.log("wxid modify failed.");
        }
      }
    });
  },
  bindInput: function (e) {
    var that = this;

    var value = e.detail.value;
    console.log(e);
    that.setData({
      input_length: value.length
    });

  }
})
